<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class AppController extends Controller {

    var $name = "App";
    var $components = array("RequestHandler", 'Session', 'Auth', 'Acl');
    var $helpers = array('Html', 'Form', 'Javascript', "Ajax", 'Session');

    function beforeFilter() {
        $this->Auth->authorize = 'actions';
        $this->Auth->actionPath = 'controllers/';
        $this->header('Pragma: no-cache');
        $this->header('Cache-control: no-cache');

        $this->Auth->allow(array('send', 'login', 'checksesion'));
        $this->Auth->loginError = 'El nombre de usuario y/o la contrase&ntilde;a no son correctos. Por favor, intente  de nuevo ';
        $this->Auth->authError = 'No tiene permiso de acceso a esta zona';
        $this->Session->write('Auth.redirect', null);

        if (!$this->RequestHandler->isAjax()) {
            ////OBTENEMOS LOS TABS
            App::import("Model", "Tab");
            $tab = new Tab();
            $tab->recursive = -1;
            if (isset($this->params['admin'])) {
                $datos = $tab->find('all', array('conditions' => array('Tab.id' => 1)));
            } else {
                $datos = $tab->find('all', array('conditions' => array('Tab.id !=' => 1), 'order' => 'Tab.order ASC'));
            }
            $this->set('lista', $datos);
            //OBTENEMOS INFORMACION DE LOS USUARIOS
            App::import("Model", "Branch");
            $br = new Branch();
            $rg = $br->field('nombre', array('Branch.id' => $this->Auth->user('branch_id')));

            App::import("Model", "Call");
            $Call = new Call();
            $Call->recursive = -1;
            $this->set('calls', $Call->find('list', array('fields' => array('Call.id', 'Call.nombre'))));
            $usr=$this->Auth->user('nombre') . " " . $this->Auth->user('primer_apellido') . " " . $this->Auth->user('segundo_apellido');
            $this->set('regional', $rg);
            $this->set('usr', $usr);
            $this->Auth->userModel = 'User';
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');

            if (isset($this->params['admin'])) {


                $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'admin_index');
                $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'admin_login');
            } else {
                $this->Auth->loginRedirect = array('controller' => 'Pages', 'action' => 'display');
                $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
            }
        }

        if (sizeof($this->uses) && $this->{$this->modelClass}->Behaviors->attached('Logable')) {
            $this->{$this->modelClass}->setUserData($this->Session->read('Auth'));
        }
    }

}
?>